#ifndef UPDATERCORE_H
#define UPDATERCORE_H

#include <QtCore>

struct InputData
{
  QString server;
  QString version_file;
  QString local_dir;
  QString architecture;
  int current_version;
  QMap<QString,QString> path_subst;
  QString run_after, run_after_dir;
};

struct ModuleInfo
{
  QString name;
  QString file_name, server_path;
  QString install_path;
};

struct VersionInfo
{
  VersionInfo()
  {
    latest_version = 0;
  }

  int latest_version;
  QString txt_version;
  QVector<ModuleInfo> modules;
};

class IUpdaterHandler
{
public:
  virtual void onModuleDownload(const ModuleInfo &mi) {}
  virtual void onModuleInstall(const ModuleInfo &mi) {}
  virtual void onDownloadFinished(int total, int loaded) {}
  virtual void onInstallFinished(int total, int installed) {}
  virtual void onFinished() {}
};

class UpdaterCore
{
public:
    UpdaterCore();
    ~UpdaterCore();

    void setHandler(IUpdaterHandler *handler) { m_handler = handler; }

    void setInputData(const InputData &data);
    const InputData& getInputData() const { return m_input; }

    int getServerVersion();
    const VersionInfo& getInfo() const { return m_info; }

    int updateAll();

    int downloadModules();
    int writeInstallList(const QString &filename);
    int installModules();
    int installModules(const QString &filename);

    void runDelayed();

protected:
    bool readVersionFile(const QString &filename);
    int installModule(const ModuleInfo &mi, QString &install_path);
    int installModule(const QString &path, const QString &install_path);

    InputData m_input;
    VersionInfo m_info;

    class Server *m_server;
    IUpdaterHandler *m_handler;
};

#endif // UPDATERCORE_H
